package com.edais.actions;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.SpreadService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;

@Controller
@RequestMapping("/spread")

public class SpreadAction  implements Serializable{
	private static final Log logger = LogFactory.getLog(SpreadAction.class);

	@Autowired
	private SpreadService spreadService;

	/** 用户点击广告
	 * @param request
	 * @return response
	 * @throws Exception */
	@RequestMapping(value = "/requestOfClickAd")
	public void requestOfClickAd(HttpServletRequest request, HttpServletResponse response){
		Map<String, String> paramsMap = new HashMap<String, String>();
		String name = "";
		String values = "";
		
		try {
			// 获取传入参数
			for (Enumeration<String> names = request.getParameterNames(); names.hasMoreElements();paramsMap.put(name, values) ) {
				name = (String) names.nextElement();
				values = request.getParameter(name);
			}
			
			// 点击广告IP地址 request.getRemoteAddr() 
			paramsMap.put("clickIp", request.getRemoteAddr());
			
			this.spreadService.userClickAd(paramsMap);
			
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE,"");
			// 处理返回结果
			CommonUtil.responseJsonNotEncrypt(result, response);
		} catch (Exception e) {
			logger.debug("/spread/requestOfClickAd 异常：" + e.getMessage() + e.getCause());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
				Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJsonNotEncrypt(result, response);
		}
	}
	
	/** 用户激活App
	 * @param request
	 * @return response
	 * @throws Exception */
	@RequestMapping(value = "/requestOfActivateApp")
	public void requestOfActivateApp(HttpServletRequest request, HttpServletResponse response){
		Map<String, String> paramsMap = new HashMap<String, String>();
		String name = "";
		String values = "";
		
		try {
			// 获取传入参数
			for (Enumeration<String> names = request.getParameterNames(); names.hasMoreElements();paramsMap.put(name, values) ) {
				name = (String) names.nextElement();
				values = request.getParameter(name);
			}
			
			// 点击广告IP地址 request.getRemoteAddr() 
			paramsMap.put("actIp", request.getRemoteAddr());
			
			this.spreadService.userActivateApp(paramsMap);
			
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.SUCCESS_CODE, 
				Consts.SUCCESS_DESCRIBE,"");
			// 处理返回结果
			CommonUtil.responseJsonNotEncrypt(result, response);
		} catch (Exception e) {
			logger.debug("/spread/requestOfActivateApp 异常：" + e.getMessage() + e.getCause());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, 
				Consts.ErrorMsg.MSG00001,
				Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJsonNotEncrypt(result, response);
		}
	}


}
